package com.lyk.coursearrange.common.exception.handler;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.lyk.coursearrange.common.exception.annotation.ExcelHeadStyle;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Component;

/**
 * 自定义excel头部样式拦截器
 * @author chenkun
 * @desc
 * @datetime 2020/9/30 14:33
 */
@Component
public class StyleExcelHandler implements SheetWriteHandler {

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Class clazz = writeSheetHolder.getClazz();
        ExcelHeadStyle annotation = (ExcelHeadStyle) clazz.getAnnotation(ExcelHeadStyle.class);
        if (annotation != null) {
            switch (annotation.type()) {
                case BASE:
                    doBase(writeWorkbookHolder, annotation);
                    break;
                case EXCEL_DSMD:
                    doInstructor(writeWorkbookHolder, annotation);
                    break;
                default:
                    // doAnything
                    break;
            }
        }

    }

    private void doInstructor(WriteWorkbookHolder writeWorkbookHolder, ExcelHeadStyle annotation) {
        Workbook workbook = writeWorkbookHolder.getWorkbook();
        Sheet sheet = workbook.getSheetAt(0);
        // 合并单元格
        CellRangeAddress cra =new CellRangeAddress(0, 0, 0, 7); // 起始行, 终止行, 起始列, 终止列
        sheet.addMergedRegion(cra);
        // 设置标题
        Row row1 = sheet.createRow(0);// 行
        row1.setHeight((short) 2200);

        Cell cell1 = row1.createCell(0);// 列
        // 标题内容（来自注解）
        cell1.setCellValue(annotation.value());
        CellStyle cellStyle = workbook.createCellStyle();
        // 文字颜色
        Font font = workbook.createFont();
        font.setColor(IndexedColors.RED.getIndex());
//            if (i == 0) {// 第一行加粗
//                font.setBold(true);
//            }
        cellStyle.setFont(font);
        // 根据手动\n换行
        cellStyle.setVerticalAlignment(VerticalAlignment.DISTRIBUTED);
        cell1.setCellStyle(cellStyle);
    }

    private void doBase(WriteWorkbookHolder writeWorkbookHolder, ExcelHeadStyle annotation) {
        Workbook workbook = writeWorkbookHolder.getWorkbook();
        Sheet sheet = workbook.getSheetAt(0);
        // 设置标题
        Row row1 = sheet.createRow(0);
        row1.setHeight((short) 600);
        Cell cell1 = row1.createCell(0);
        // 标题内容（来自注解）
        cell1.setCellValue(annotation.value());
        CellStyle cellStyle = workbook.createCellStyle();
//        自动换行
//        cellStyle.setWrapText(true);

        // 文字颜色
        Font font = workbook.createFont();
        font.setColor(IndexedColors.RED.getIndex());
        cellStyle.setFont(font);
        // 平铺展开
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cell1.setCellStyle(cellStyle);
    }
}
